home *** CD-ROM | disk | FTP | other *** search
Text File | 1998-09-03 | 33.4 KB | 1,161 lines | [TEXT/MPS ] |
- /*
- File: Chooser.cp
-
- Contains: Chooser PACK code to support USB and serial printers
-
- Written by: G. Poon
-
- Copyright: © 1998 by Apple Computer, Inc., all rights reserved.
-
- Change History:
-
- 10 Jun 98 gp Lock our global area
- 9 Jun 98 gp Clear the handle when creating the global storage
- Remove old global rsrc if its still there
- 8 May 98 gp If we don't support USB then no need to init the name registry
- ptrs. This will prevent the name registry alert from
- displaying on non PCI PowerMacs if we only support serial.
- 8 May 98 gp Moved creation of model index array to InitPack
- 21 Apr 98 gp If only 1 USB printer is listed and no previous selection
- then select it. Added new routines to save selected printer's
- name and USB path If previously selected printer is not available
- display an alert.
- 27 Mar 98 gp Routines to access connection info from in rsrc
- 26 Mar 98 gp Release model index storage. Add support for serial ports
- 25 Mar 98 gp Change code to store our global space our rsrc
- 21 Mar 98 gp Created
-
- To Do:
- */
-
- #ifndef __Chooser__
- #include "Chooser.h"
- #endif
-
- #ifndef __COMMON__
- #include "Common.h"
- #endif
-
- #ifndef __UTILS__
- #include "Utils.h"
- #endif
-
- #ifndef __DEVICES__
- #include <Devices.h>
- #endif
-
- #ifndef __RESOURCES__
- #include <Resources.h>
- #endif
-
- #ifndef __TEXTUTILS__
- #include <TextUtils.h>
- #endif
-
- #ifndef __COMMRESOURCES__
- #include <CommResources.h>
- #endif
-
- #ifndef __CRMSERIALDEVICES__
- #include <CRMSerialDevices.h>
- #endif
-
- #ifndef __MIXEDMODE__
- #include <MixedMode.h>
- #endif
-
- #ifndef __CODEFRAGMENTS__
- #include <CodeFragments.h>
- #endif
-
- #ifndef __DIALOGS__
- #include <dialogs.h>
- #endif
-
- #ifndef __SafeNameRegistry__
- #include "SafeNameRegistry.h"
- #endif
-
- /******************************************************************************
- Typedefs
- ******************************************************************************/
-
- // This structure is passed to AddPrintertoList (a callback routine) from
- // SearchForUSBPrinters. Its used to hold data which is needed to add a
- // printer name to the Chooser's printer list
- typedef struct
- {
- ListHandle list; // the printer list
- Cell insertcell; // where to insert an entry
- short modelIndex; // index into STR# holding printer models supported
- } AddPrintertoListCallBackStruct, *AddPrintertoListCallBackStructPtr;
-
- // This structure is passed to AddPorttoList (a callback routine) from
- // SearchForSerialPorts. Its used to hold data which is needed to add a
- // serial port to the Chooser's printer list
- typedef struct
- {
- ListHandle list; // the printer list
- Cell insertcell; // where to insert an entry
- } AddPorttoListCallBackStruct, *AddPorttoListCallBackStructPtr;
-
- // typedef for callback routine used in SearchForUSBPrinters when a printer
- // entry is found in the name registry
- typedef void (*FoundUSBPrinterProcPtr) ( RegEntryID* aPrinterEntry, void* userData);
-
- // typedef for callback routine used in SearchForSerialPorts when a port
- // entry is found in the name registry
- typedef void (*FoundSerialPortProcPtr) ( CRMSerialPtr aPort, void* userData);
-
- /******************************************************************************
- Prototypes
- ******************************************************************************/
-
- short CountUSBPrinters(void);
- void FillPrinterList( ListHandle list );
- void AddPrintertoList( RegEntryID* aPrinterEntry, void* userData );
- void CountAPrinter( RegEntryID* aPrinterEntry, void* userData );
- void SearchForUSBPrinters( StringPtr modelPath, FoundUSBPrinterProcPtr callback, void* userData );
-
- short CountSerialPorts(void);
- void AddPorttoList( CRMSerialPtr aPort, void* userData );
- void CountAPort( CRMSerialPtr aPort, void* userData );
- void SearchForSerialPorts(FoundSerialPortProcPtr callback, void* userData);
-
- OSErr ShowSelection (ListHandle list, StringPtr zoneName);
- OSErr Select (ListHandle list, StringPtr zoneName, long rowNum);
-
- void SetConnectionType(short type);
- short GetConnectionType(void);
- OSErr InitPack(void);
-
- Boolean SaveSelectedUSBPrinter( Cell selectedCell, ListHandle list );
- void SaveSelectedPrinterName( Cell selectedCell, ListHandle list);
- Handle GetLastSelectedUSBPrinter();
-
- /******************************************************************************
- Constants
- ******************************************************************************/
-
- #define kHilited 0
- #define kUnHilited 255
-
- #define kGlobalType 'wxyz' // made up type
- #define kGlobalID 128
-
- #define kConnectionType 'CTYP'
- #define kConnectionTypeID 1000
-
- // Possible selected types. Used in ConnectionType rsrc
- #define kNone 0
- #define kSerial 1
- #define kUSB 2
-
- #define kAPortName "\p.AIn" // To compare input device name when searching serial devices in the Comm Tool Box's List
- #define kBPortName "\p.BIn" // To comapre input device name when searching serial devices in the Comm Tool Box's List
-
- #define kPrinterNotAvailable 3100
- /*-----------------------------------------------------------------------------*
-
- SaveSelectedUSBPrinter
-
- Desc: Saves the name registry path of the selected printer
-
- In: - the cell of the selected printer
- - the Chooser's printer list
- - the row of the selected cell
-
- Out: true if the path was saved
- false if the path wasn't saved
-
- History:
-
- 21 Apr 98 gp Created
-
- *-----------------------------------------------------------------------------*/
- Boolean SaveSelectedUSBPrinter( Cell selectedCell, ListHandle list )
- {
- Handle lastPrinterString=nil; // the previous selected printer
- Str255 selectedPrinterName; // name of the selected printer
- Str255 modelString; // path in name registry of printer model
- Boolean saved=false; // was the selected printer saved properly
-
- USBGlobalsHandle gGlobals=nil; // our global data area
-
- gGlobals = GetGlobalStorage();
-
- // Get the name of the printer that was selected out of the list
- GetNameFromCell (selectedPrinterName, selectedCell, list);
- // get the last selected printer
- lastPrinterString = GetLastSelectedUSBPrinter();
-
- // get the model path
- GetIndString(modelString, kUSBModelPathString, *((**gGlobals).modelIndex+selectedCell.v) );
- if( modelString[0] != 0 && lastPrinterString != nil)
- {
- Handle tempHandle=nil;
- // create the selected printer's name registry path
- AppendPStr( (StringPtr) modelString, "\p:" );
- AppendPStr( (StringPtr) modelString, selectedPrinterName );
-
- HLock( lastPrinterString );
- BlockMove( modelString, *lastPrinterString, modelString[0]+1 );
- HUnlock( lastPrinterString );
-
- // save the selected printers's name registry path
- ChangedResource( lastPrinterString );
- SetConnectionType( kUSB );
-
- WriteResource(lastPrinterString);
- saved = true;
- }
- return saved;
- }
-
- /*-----------------------------------------------------------------------------*
-
- GetLastSelectedUSBPrinter
-
- Desc: Returns the name registry path of the last selected USB printer
-
- In: None
-
- Out: Handle to a STR containing the name registry path of the last
- selected USB printer
-
- History:
-
- 21 Apr 98 gp Created
-
- *-----------------------------------------------------------------------------*/
- Handle GetLastSelectedUSBPrinter()
- {
- Handle printerString=nil; // the full path in name registry of last selected USB printer
-
- printerString = Get1Resource( 'STR ', kUSBPrinterPathString );
- if( printerString == nil ) {
- DebugStr("\pIn GetLastSelectedUSBPrinter. THIS SHOULD NEVER HAPPEN");
- }
- return printerString;
- }
-
- /*-----------------------------------------------------------------------------*
-
- SaveSelectedPrinterName
-
- Desc: Saves the name of the selected printer in our rsrc
-
- In: - the cell of the selected printer
- - the Choosers printer list handle
-
- Out: None
-
- History:
-
- 21 Apr 98 gp Created
-
- *-----------------------------------------------------------------------------*/
- void SaveSelectedPrinterName( Cell selectedCell, ListHandle list)
- {
- Handle printerString=nil; // path in name registry of current USB printer
- Str255 selectedPrinterName; // name of the selected printer
-
- GetNameFromCell (selectedPrinterName, selectedCell, list);
-
- // save the name of the printer
- printerString = (Handle) Get1Resource( 'STR ', kPrinterNameString );
- if( selectedPrinterName[0] != 0 && printerString != nil)
- {
- HLock( printerString );
- BlockMove( selectedPrinterName, *printerString, selectedPrinterName[0]+1 );
- HUnlock( printerString );
-
- // save the newly selected printer's name
- ChangedResource( printerString );
- WriteResource(printerString);
- }
-
- }
-
- /*-----------------------------------------------------------------------------*
-
- GetConnectionType
-
- Desc: Gets the connection type for the last selected printer
- from our rsrc
-
- In: None
-
- Out: kNone, kSerial or kUSB
-
- History:
-
- 27 Mar 98 gp Created
-
- *-----------------------------------------------------------------------------*/
- short GetConnectionType(void)
- {
- Handle connection;
-
- connection = (Handle) Get1Resource( kConnectionType, kConnectionTypeID );
- return (**(short**)connection);
-
- }
-
- /*-----------------------------------------------------------------------------*
-
- SetConnectionType
-
- Desc: Saves the connection type of the selected printer in our rsrc
-
- In: kNone, kSerial or kUSB
-
- Out: None
-
- History:
-
- 27 Mar 98 gp Created
-
- *-----------------------------------------------------------------------------*/
- void SetConnectionType(short type)
- {
- Handle connection;
-
- connection = (Handle) Get1Resource( kConnectionType, kConnectionTypeID );
- (**(short**)connection) = type;
- ChangedResource( connection );
- WriteResource( connection );
- }
-
- /*-----------------------------------------------------------------------------*
-
- CountAPort
-
- Desc: Callback routine for CountSerialPorts. It increments the
- ptr to user data thus incrementing the count of serial ports.
-
- In: - A port to a CRMSerialPtr struct
- - A ptr to user data
-
- Out: None
-
- History:
-
- 26 Mar 98 gp Created
-
- *-----------------------------------------------------------------------------*/
- void CountAPort( CRMSerialPtr aPort, void* userData )
- {
- // we're only interested in .ain and .bin
- if ( (PStrEqualCaseInsensitive((unsigned char*) kBPortName, *(aPort->inputDriverName)) ) ||
- (PStrEqualCaseInsensitive((unsigned char*) kAPortName, *(aPort->inputDriverName)) ))
- {
- *((short*) userData) +=1;
- }
- }
-
- /*-----------------------------------------------------------------------------*
-
- AddPorttoList
-
- Desc: Adds a serial port the the Chooser's list. We're only interested
- in modem and printer ports
-
- In: - A port to a CRMSerialPtr struct
- - A ptr to user data. This should be a ptr to a
- AddPorttoListCallBackStruct
-
- Out: None
-
- History:
-
- 26 Mar 98 gp Created
-
- *-----------------------------------------------------------------------------*/
- void AddPorttoList( CRMSerialPtr aPort, void* userData )
- {
- AddPorttoListCallBackStructPtr addPortStruct=(AddPorttoListCallBackStructPtr) userData;
-
- // we're only interested in .ain and .bin
- if ( PStrEqualCaseInsensitive((unsigned char*) kBPortName, *(aPort->inputDriverName)) )
- {
- LAddRow(1, addPortStruct->insertcell.v, addPortStruct->list);
- LSetCell( "Printer", 7, addPortStruct->insertcell, addPortStruct->list );
- addPortStruct->insertcell.v++;
- }
-
- if ( PStrEqualCaseInsensitive((unsigned char*) kAPortName, *(aPort->inputDriverName)) )
- {
- LAddRow(1, addPortStruct->insertcell.v, addPortStruct->list);
- LSetCell( "Modem", 5, addPortStruct->insertcell, addPortStruct->list );
- addPortStruct->insertcell.v++;
- }
- }
-
- /*-----------------------------------------------------------------------------*
-
- CountSerialPorts
-
- Desc: Returns the number of serial ports
-
- In: None
-
- Out: The number of serial ports
-
- History:
-
- 26 Mar 98 gp Created
-
- *-----------------------------------------------------------------------------*/
- short CountSerialPorts(void)
- {
- short numberOfPorts=0;
-
- SearchForSerialPorts( (FoundSerialPortProcPtr) &CountAPort, &numberOfPorts );
-
- return numberOfPorts;
- }
-
- /*-----------------------------------------------------------------------------*
-
- SearchForSerialPorts
-
- Desc: Searchs for serial ports on this machine
-
- In: - A callback routine which gets called when a printer is found
- - A ptr to user data
-
- Out: None
-
- History:
-
- 26 Mar 98 gp Created
-
- *-----------------------------------------------------------------------------*/
- void SearchForSerialPorts(FoundSerialPortProcPtr callback, void* userData)
- {
- CRMErr theErr; // comm rsrc manager error
- CRMRec commRec; // communications resource manager record
- CRMRecPtr commRecPtr; // ptr to a communication resource record in the queue
- CRMSerialPtr commRecSerialPtr; // ptr to serial record inside comm resource record
- long curSerialDeviceID = 0; // initially set to zero, so we get all serial devices
-
- theErr = InitCRM(); // Initialize the Communications Resource Manager
-
- // now search for serial devices
- while (theErr == noErr)
- {
- commRec.crmDeviceType = crmSerialDevice; // look for serial devices
- commRec.crmDeviceID = curSerialDeviceID; // look for a device number greater than curSerialDeviceID
-
- commRecPtr = &commRec;
- commRecPtr = CRMSearch(commRecPtr); // Search queue for each serial device
-
- if (commRecPtr != nil)
- {
- // get the Serial record pointer
- commRecSerialPtr = (CRMSerialPtr)(*commRecPtr).crmAttributes;
-
- callback( commRecSerialPtr, userData); // found a port
-
- // Set device ID for next search
- curSerialDeviceID = (*commRecPtr).crmDeviceID;
- }
- else
- {
- theErr = 1; // game over, no more serial devices
- }
- }
- }
-
- /*-----------------------------------------------------------------------------*
-
- SearchForUSBPrinters
-
- Desc: Searches thru a printer model entry in the name registry
- looking for a model's printers. When one is found a call is
- issued to the clients callback routine for further processing
-
- In: - A pstring full path in the name registry of a printer model
- - A callback routine which gets called when a printer is found
- - A ptr to user data
-
- Out: None
-
- History:
-
- 24 Feb 98 gp Created
-
- *-----------------------------------------------------------------------------*/
- void SearchForUSBPrinters( StringPtr modelPath, FoundUSBPrinterProcPtr callback, void* userData )
- {
-
- RegEntryID theModelEntry; // the model node supported by the driver
- RegEntryID aPrinterEntry; // a printer node in the name registry
- RegEntryIterationOp iterOp; // name registry iterator op code
- RegEntryIter printerIterator; // used to iterate child nodes of printer model
- Boolean donePrinters = false; // NameRegistry param tell when we're done
- OSStatus err = noErr; // error from name registry calls
-
- // name registry only deals with c strings
- p2cstr( (StringPtr) modelPath );
-
- // look up the node for the printer model requested
- err = SafeRegistryEntryIDInit(&theModelEntry);
- err = SafeRegistryCStrEntryLookup( nil, (char*) modelPath, &theModelEntry );
-
- if( err == noErr )
- {
- // create an iterator to look at the child nodes for our printer model entry
- iterOp = kRegIterChildren;
-
- err = SafeRegistryEntryIterateCreate( &printerIterator );
- err = SafeRegistryEntryIterateSet(&printerIterator, &theModelEntry);
-
- if( err == noErr )
- {
- // look for a model's connected printers
- do
- {
- err = SafeRegistryEntryIterate( &printerIterator, iterOp, &aPrinterEntry, &donePrinters );
- if( !donePrinters && err == noErr )
- callback(&aPrinterEntry, userData); // found a printer
-
- iterOp = kRegIterContinue;
- } while( !donePrinters && err == noErr );
- // end while for printers
- }
- SafeRegistryEntryIterateDispose(&printerIterator);
- }
-
- SafeRegistryEntryIDDispose( &theModelEntry );
- c2pstr( (char*) modelPath );
-
- }
-
- /*-----------------------------------------------------------------------------*
-
- CountAPrinter
-
- Desc: Callback routine for CountUSBPrinters. It increments the
- ptr to user data thus incrementing the count of USB printers.
-
- In: - A printer entry in the name registry
- - A ptr to user data
-
- Out: The number of USB printers in the name registry
-
- History:
-
- 24 Feb 98 gp Created
-
- *-----------------------------------------------------------------------------*/
-
- void CountAPrinter( RegEntryID* aPrinterEntry, void* userData )
- {
- *((short*) userData) +=1;
- }
-
- /*-----------------------------------------------------------------------------*
-
- CountUSBPrinters
-
- Desc: Returns the number of connected USB printers associated with this driver
-
- In: None
-
- Out: The number of USB printers in the name registry
-
- History:
-
- 24 Feb 98 gp Created
-
- *-----------------------------------------------------------------------------*/
-
- short CountUSBPrinters()
- {
- short numberOfPrinters=0; // return value for number of USB printers
- Str255 modelPath; // pstring path in a supported printer model
- short i; // counter
-
- i=1;
- // read a supported printer's name registry model path
- GetIndString(modelPath, kUSBModelPathString, i);
-
- while( (char*) modelPath[0] != 0 ) // if len is zero then no more models
- {
- SearchForUSBPrinters( modelPath, (FoundUSBPrinterProcPtr) &CountAPrinter, &numberOfPrinters );
- i++;
- // read another supported printer's name registry model path
- GetIndString(modelPath, kUSBModelPathString, i);
- }
-
- return numberOfPrinters;
- }
-
- /*-----------------------------------------------------------------------------*
-
- AddPrintertoList
-
- Desc: Adds an USB printer to the Chooser's printer list by extracting
- the name from the name registry
-
- In: - A printer entry in the name registry
- - A ptr to user data. This should be a ptr to a
- AddPrintertoListCallBackStruct
-
- Out: The number of USB printers in the name registry
-
- History:
-
- 24 Feb 98 gp Created
-
- *-----------------------------------------------------------------------------*/
- void AddPrintertoList( RegEntryID* aPrinterEntry, void* userData )
- {
- OSStatus err = noErr; // error from name registry call
- Str255 nodeName; // the name of the printer
- RegPropertyValueSize nameSize; // size of name buffer
- AddPrintertoListCallBackStructPtr fillListStruct=(AddPrintertoListCallBackStructPtr) userData;
- USBGlobalsHandle gGlobals=nil; // our global data area
-
- gGlobals = GetGlobalStorage();
-
- nameSize = sizeof( nodeName );
- // grab name of printer in name registyr
- err = SafeRegistryPropertyGet( aPrinterEntry, "name", &nodeName, &nameSize );
-
- if( err == noErr ) {
- // stick it in the printer list
- LAddRow(1, fillListStruct->insertcell.v, fillListStruct->list);
- LSetCell( nodeName, nameSize-1, fillListStruct->insertcell, fillListStruct->list );
-
- // save info about which model (STR#) this printer belongs to
- *((**gGlobals).modelIndex+fillListStruct->insertcell.v) = fillListStruct->modelIndex;
- fillListStruct->insertcell.v++;
- }
- }
-
- /*-----------------------------------------------------------------------------*
-
- FillUSBList
-
- Desc: Fills the printer list with the number of USB printers
-
- In: - The Chooser's printer list handle
-
- Out: None
-
- History:
-
- 24 Feb 98 gp Created
-
- *-----------------------------------------------------------------------------*/
- void FillPrinterList( ListHandle list )
- {
- AddPrintertoListCallBackStruct userData; // struct needed by callback routine
- AddPorttoListCallBackStruct userPortData; // struct needed by callback routine
- Str255 modelPath; // pstring path in a supported printer model
- short i; // counter
-
- USBGlobalsHandle gGlobals=nil; // our global data area
-
- gGlobals = GetGlobalStorage();
-
- userPortData.insertcell.v = 0;
- // if we support serial ports then list serial ports
- if( (**gGlobals).supportsSerial == true ) {
- userPortData.list = list;
- userPortData.insertcell.h = 0;
-
- // add the serial ports to the chooser list
- SearchForSerialPorts( (FoundSerialPortProcPtr) &AddPorttoList, (void*) &userPortData );
- }
-
- // if we support USB then list USB printers
- if( (**gGlobals).supportsUSB == true ) {
- userData.list = list;
- userData.insertcell.v = userPortData.insertcell.v;
- userData.insertcell.h = 0;
-
- i=1;
- // read a supported printer's name registry model path
- GetIndString(modelPath, kUSBModelPathString, i);
- while( (char*) modelPath[0] != 0 ) // if len is zero then no more models
- {
- userData.modelIndex = i;
- // add the USB printers to the chooser list
- SearchForUSBPrinters( modelPath, (FoundUSBPrinterProcPtr) &AddPrintertoList, (void*) &userData );
- i++;
- // read another supported printer's name registry model path
- GetIndString(modelPath, kUSBModelPathString, i);
- }
- }
- }
-
- /*-----------------------------------------------------------------------------*
-
- ShowSelection
-
- Desc: Hilites the currently selected printer in the printer list
-
- In: - Handle to Mac List Mgr list
- - Name of zone we are currently in
-
- Out: OS error if any
-
- History:
-
- 21 Apr 98 gp If only 1 USB printer is listed and no previous selection
- then select it. If previously selected printer is not available
- display an alert.
- 9 Apr 98 gp Use hlock not hunlock. Moved foundcell outside loop
- 21 Mar 98 gp Extract previously selected printer from our rsrc
- 24 Feb 98 gp Created
-
- *-----------------------------------------------------------------------------*/
- OSErr ShowSelection (ListHandle list, StringPtr zoneName)
- {
- Cell cell; // used to iterate thru printer list, shows selected cell
- Str255 nameBuffer; // selected AT printer is on the net, search for the cell
- Boolean foundCell = false; // found a cell with same name as selected printer
- Handle selectedPrinter; // name of the selected printer
-
- Str255 modelString; // path in name registry of printer model
- Handle printerString; // the full path in name registry of selected USB printer
- RegEntryID thePrinterEntry; // the node of the USB printer node selected
- OSStatus err = noErr; // err encountered when calling name registry API
- short hiliteState = kUnHilited; // hilite state of the button
- USBGlobalsHandle gGlobals=nil; // our global data area
- short connectionType; // what type of printer connection, serial or USB
-
- gGlobals = GetGlobalStorage();
-
- // get the current selected printer out of the rsrc
- printerString = GetLastSelectedUSBPrinter();
- if( printerString == nil ) {
- return noErr;
- }
-
- // extract printer name for use later
- selectedPrinter = (Handle) Get1Resource( 'STR ', kPrinterNameString );
- if( selectedPrinter == nil)
- return noErr;
- HLock( selectedPrinter );
-
- connectionType = GetConnectionType();
- switch( connectionType ) {
- case kSerial:
- SetPt (&cell, 0, 0);
-
- // look for the port
- while ( (!foundCell) && (cell.v < (*list)->dataBounds.bottom) )
- {
- GetNameFromCell(nameBuffer, cell, list);
-
- // if it equals the name of the current cell, we found the printer
- if ( EqualString( (StringPtr) *selectedPrinter, nameBuffer, true, true) ) {
- foundCell = true;
- } else
- cell.v++;
- }
- if( !foundCell )
- StopAlert(kPrinterNotAvailable, nil);
- break;
- case kUSB:
- HLock( printerString );
-
- // name registry only deals with c strings
- p2cstr( (StringPtr) *printerString );
-
- // check to make sure printer is still in name registry before selecting
- SafeRegistryEntryIDInit(&thePrinterEntry);
- err = SafeRegistryCStrEntryLookup( nil, (char*) *printerString, &thePrinterEntry );
-
- // clean up
- c2pstr( (char*) *printerString );
- HUnlock( printerString );
- SafeRegistryEntryIDDispose( &thePrinterEntry );
-
- // if printer is no longer connected put up an alert
- if( err ) {
- StopAlert(kPrinterNotAvailable, nil);
- } else {
- // since user could have connected or disconnect other printers from USB bus
- // we need to find the exact place in the list and hilite it
-
- SetPt (&cell, 0, 0);
-
- // loop through all the printers in the list seeing if there is a
- // match for the driver's selected printer
- while ( (!foundCell) && (cell.v < (*list)->dataBounds.bottom) )
- {
- GetNameFromCell(nameBuffer, cell, list);
-
- // If the name is one of our reserved rows and
- // it equals the name of the current cell, we found a possible printer
-
- if ( EqualString( (StringPtr) *selectedPrinter, nameBuffer, true, true) ) {
-
- // create name registry full path for this printer and compare it
- // to the one thats stored in our rsrc
-
- // get the printers model
- GetIndString(modelString, kUSBModelPathString, *((**gGlobals).modelIndex+cell.v) );
- if( modelString[0] != 0 )
- {
- // create the path
- AppendPStr( (StringPtr) modelString, "\p:" );
- AppendPStr( (StringPtr) modelString, nameBuffer );
- // check to see if its the same
- HLock( printerString );
- if ( EqualString( modelString, (StringPtr) *printerString, true, true) )
- foundCell = true;
- HUnlock( printerString );
- }
- }
-
- if (!foundCell)
- cell.v++;
- } // end while
- } // end else
- break;
- default:
- break;
- }
- HUnlock( selectedPrinter );
-
- // if this is our first time and only 1 entry in the list and its USB then select it
- if( ((**gGlobals).supportsUSB == true) && !foundCell &&
- ((*list)->dataBounds.bottom == 1) && ((**gGlobals).numberOfUSBPrinters == 1) )
- {
- if( printerString != nil && (**printerString) == 0x0) {
- SetPt(&cell, 0, 0); // select the 1st cell
-
- if( SaveSelectedUSBPrinter( cell, list ) ) {
- // save the name of the printer
- SaveSelectedPrinterName( cell, list );
- foundCell = true;
- }
- }
- }
-
- // now hilite the selection
- if (foundCell)
- {
- LSetSelect(true, cell, list);
- hiliteState = kHilited;
- }
-
-
- return noErr;
- }
-
- /*-----------------------------------------------------------------------------*
-
- Select
-
- Desc: Save the selected printers name and name registry path in our rsrc
-
- In: - Handle to Mac List Mgr list
- - Name of zone we are currently in
- - the row Chooser thinks we should select.
-
- Out: OS error if any
-
- History:
-
- 21 Mar 98 gp Save printer name in our rsrc
- 24 Feb 98 gp Created
-
- *-----------------------------------------------------------------------------*/
- OSErr Select (ListHandle list, StringPtr zoneName, long rowNum)
- {
-
- Str255 modelString; // path in name registry of printer model
- Handle printerString=nil; // path in name registry of current USB printer
- Cell selectedCell; // for list processing
- Str32 selectedPrinterName; // select printer in list
- USBGlobalsHandle gGlobals=nil; // our global data area
-
- gGlobals = GetGlobalStorage();
-
- // Find out which row was selected
- SetPt (&selectedCell, 0, 0);
- LGetSelect (true, &selectedCell, list);
-
- // Get the name of the printer that was selected out of the list
- GetNameFromCell (selectedPrinterName, selectedCell, list);
-
- // if nothing selected leave
- if( selectedPrinterName[0] == 0 )
- return noErr;
-
- // serial port selected so just clear the USB model path in the STR rsrc
- if( selectedCell.v < (**gGlobals).numberOfPorts ) {
- printerString = GetLastSelectedUSBPrinter();
- (**printerString) = 0;
- ChangedResource( printerString );
- SetConnectionType( kSerial );
- WriteResource(printerString);
- } else {
- SaveSelectedUSBPrinter( selectedCell, list );
- }
-
- // save the name of the printer
- SaveSelectedPrinterName( selectedCell, list );
- return noErr;
- }
-
- /*-----------------------------------------------------------------------------*
-
- InitPack
-
- Desc: Creates our global storage and reads info out of our
- connection info rsrc
-
- In: None
-
- Out: OS error if any
-
- History:
-
- 8 May 98 gp If we don't support USB then no need to init the name registry
- ptrs. This will prevent the name registry alert from
- displaying on non PCI PowerMacs if we only support serial.
- 8 May 98 gp Moved creation of model index to here from InitGlobalStorage
- 21 Apr 98 gp If we don't support serial set numberOfPorts to zero
- 27 Mar 98 gp Created
-
- *-----------------------------------------------------------------------------*/
- OSErr InitPack(void)
- {
- OSErr returnValue = noErr; // return value
- USBGlobalsHandle gGlobals = nil; // our global data area
- ConnectionTypeHdl connectionInfo = nil;
- short numberOfUSBPrinters = 0; // number of USB printers detected
- short numberOfPorts = 0; // number of serial ports
-
- // initialize our global storage
- returnValue = InitGlobalStorage();
- if( returnValue == noErr )
- {
- gGlobals = GetGlobalStorage();
- connectionInfo = (ConnectionTypeHdl) Get1Resource( kConnectionType, kConnectionTypeID );
- if( connectionInfo != nil )
- {
- (**gGlobals).supportsSerial = ( (**connectionInfo).supportsSerial == 1 );
- (**gGlobals).supportsUSB = ( (**connectionInfo).supportsUSB == 1 );
-
- // if we don't support serial ports then zero out serial port count
- if( (**gGlobals).supportsSerial == false )
- (**gGlobals).numberOfPorts = 0;
- }
-
- // count USB printers
- if( (**gGlobals).supportsUSB == true )
- {
- // if we support USB we need to init proc ptrs before calling CountUSBPrinters
- InitNameRegistryPtrs();
-
- numberOfUSBPrinters = CountUSBPrinters();
- (**gGlobals).numberOfUSBPrinters = numberOfUSBPrinters;
- } else {
- numberOfUSBPrinters = 0;
- (**gGlobals).numberOfUSBPrinters = 0;
- }
-
- // count serial ports
- if( (**gGlobals).supportsSerial == true ) {
- numberOfPorts = CountSerialPorts();
- (**gGlobals).numberOfPorts = numberOfPorts;
- } else {
- numberOfPorts = 0;
- (**gGlobals).numberOfPorts = 0;
- }
-
- // allocate our index storage
- // there is index space for serial ports which are unused but make the code simpler
- (**gGlobals).modelIndex = (short*) NewPtr( (numberOfPorts+numberOfUSBPrinters)*sizeof(short) );
- if ( (**gGlobals).modelIndex == nil )
- {
- // no index storage so get rid of our globals too
- RemoveGlobalStorage();
- returnValue = memFullErr;
- }
-
- }
- return returnValue;
- }
-
- /*-----------------------------------------------------------------------------*
-
- Chooser
-
- Desc: Routines to handle calls from the Chooser
-
- In: - message from chooser
- - application id
- - additional info (varies)
- - name of the appletalk zone
- - handle to device choices ( printers )
- - additional info (varies)
-
- Out: OS error if any
-
- History:
-
- 19 Mar 98 gp Created
-
- *-----------------------------------------------------------------------------*/
- pascal OSErr Chooser (short message, short caller, StringPtr objName, StringPtr zoneName, long p1, long p2)
- {
- USBGlobalsHandle gGlobals=nil; // our global data area
- OSErr returnValue = noErr; // return value
-
- gGlobals = GetGlobalStorage();
- switch (message)
- {
- case chooserInitMsg:
- returnValue = InitPack();
- break;
- case fillListMsg:
- if( gGlobals != nil )
- FillPrinterList( (ListHandle) p1 );
- break;
- case getSelMsg:
- if( gGlobals != nil )
- ShowSelection ( (ListHandle)p1, zoneName);
- break;
- case selectMsg:
- if( gGlobals != nil )
- Select ( (ListHandle)p1, zoneName, p2);
- break;
- case buttonMsg:
- break;
- case terminateMsg:
- RemoveNameRegistryPtrs();
- RemoveGlobalStorage();
- break;
- case deselectMsg:
- break;
- default:
- break;
- }
- return returnValue;
- }
-
- /*-----------------------------------------------------------------------------*
-
- InitGlobalStorage
-
- Desc: Creates and stores a block of memory in our rsrc to be
- used as global storage
-
- In: none
-
- Out: OS error if any
-
- History:
-
- 10 Jun 98 gp Lock our global area
- 9 Jun 98 gp Clear the handle when creating the global storage
- Remove old global rsrc if its still there
- 8 May 98 gp Moved creation of model index to InitPack
- 25 Mar 98 gp Created
-
- *-----------------------------------------------------------------------------*/
- OSErr InitGlobalStorage(void)
- {
- USBGlobalsHandle gGlobals=nil; // our global data area
- Handle globalrsrc; // handle to store our global data address
-
- // get rid of old global rsrc if its still there
- globalrsrc = (Handle) Get1Resource(kGlobalType, kGlobalID);
- if( globalrsrc != nil ) {
- RemoveResource( globalrsrc );
- DisposeHandle( globalrsrc );
- }
-
- // allocate our global storage
- gGlobals = (USBGlobalsHandle) NewHandleClear( sizeof( USBGlobals ) );
- globalrsrc = NewHandleClear( sizeof( Handle ) );
- if (gGlobals != nil)
- {
- HLockHi( (Handle) gGlobals );
- // save our global handle address in our rsrc
- (** (Handle**) globalrsrc) = (char**) gGlobals;
- addresource( globalrsrc, kGlobalType, kGlobalID,(char*) "\p");
-
- return noErr;
- } else
- return memFullErr;
- }
-
- /*-----------------------------------------------------------------------------*
-
- GetGlobalStorage
-
- Desc: Retrieves our global stoarge area
-
- In: none
-
- Out: Handle to our global storage
-
- History:
-
-
- *-----------------------------------------------------------------------------*/
- USBGlobalsHandle GetGlobalStorage(void)
- {
- Handle ourRsrc=nil; // handle to ourselves
-
- ourRsrc = (Handle) Get1Resource(kGlobalType, kGlobalID);
- if( ourRsrc == nil )
- return nil;
-
- return (**(USBGlobalsHandle**)ourRsrc);
- }
-
- /*-----------------------------------------------------------------------------*
-
- RemoveGlobalStorage
-
- Desc: Removes our global storage area
-
- In: none
-
- Out: Handle to our global storage
-
- History:
-
- 9 Jun 98 gp Dispose of our global rsrc after removing it
- 26 Mar 98 gp Release model index storage
- 25 Mar 98 gp Created
-
- *-----------------------------------------------------------------------------*/
- void RemoveGlobalStorage(void)
- {
- USBGlobalsHandle gGlobals=nil; // our global data area
- Handle ourRsrc;
-
- gGlobals = GetGlobalStorage();
- if( gGlobals != nil ) {
- // dispose of index ptr
-
- if ( (**gGlobals).modelIndex != nil )
- {
- DisposePtr( (Ptr) (**gGlobals).modelIndex );
- }
-
- // remove our global handle
- HUnlock( (Handle) gGlobals );
- DisposeHandle( (Handle) gGlobals );
- // remove our rsrc for global address storage
- ourRsrc = (Handle) Get1Resource(kGlobalType, kGlobalID);
- RemoveResource( ourRsrc );
- DisposeHandle( ourRsrc );
- }
- }
- // eof
-